﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsMobile.Forms;
using System.Windows.Forms;

namespace Handrise.Mobile.BasicModal.Camera
{
    public class CameraService
    {
        /// <summary>
        /// 拍摄照片
        /// </summary>
        /// <returns>拍摄后的照片路径(取消拍摄时返回空)</returns>
        /// <result>
        /// 对应保存路径下增加了照片文件
        /// </result>
        public string TakePicutes(string photoFullFileName)
        {
            string picturePath = string.Empty;

            using (CameraCaptureDialog cameraCapture = new CameraCaptureDialog())
            {
                string directory = System.IO.Path.GetDirectoryName(photoFullFileName);
                if (directory == "")
                    directory = GetLocalPath();
                cameraCapture.Owner = null;
                cameraCapture.StillQuality = CameraCaptureStillQuality.High;
                cameraCapture.InitialDirectory = directory;
                cameraCapture.DefaultFileName = System.IO.Path.GetFileName(photoFullFileName) ;// guidID + ".jpg";
                cameraCapture.Mode = CameraCaptureMode.Still;
                cameraCapture.Title = "照片采集";

                if (cameraCapture.ShowDialog() == DialogResult.OK)
                {
                    picturePath = cameraCapture.InitialDirectory + "\\" + cameraCapture.DefaultFileName;
                }
            }
            GC.Collect();
            return picturePath;
        }

        private string GetLocalPath()
        {
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

            if (path.StartsWith("file:\\"))
                return path.Remove(0, 6);
            else
                return path;
        }
    }
    
}
